package com.yw.wanandroid.ui.v

import android.Manifest
import android.app.Activity
import android.content.ContentUris
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import androidx.annotation.RequiresApi
import com.yw.wanandroid.ui.adapter.MainPagerAdapter
import com.yw.wanandroid.R
import com.yw.wanandroid.base.BaseActivity
import com.yw.wanandroid.databinding.ActivityMainBinding

class MainActivity : BaseActivity<ActivityMainBinding>() {
    private val TAG = "MainActivity"
    val permissions = arrayOf(
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.apply {
            ayMainViewpager.adapter = MainPagerAdapter(this@MainActivity, HomeFragment())
            startPlayVideo.setOnClickListener {
                startActivity(Intent(this@MainActivity, PlayVideoActivity::class.java))
//                qureImages()
                Log.d(TAG, "onCreate: externalCacheDir" + externalCacheDir?.absolutePath)
            }
            requestPermissions(permissions, 1)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun getActivity(): Activity {
        return this
    }

    fun qureImages() {
        val tableUrl = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
        val projection = arrayOf(
            MediaStore.Video.Media.DISPLAY_NAME,
            MediaStore.Video.Media.DURATION,
            MediaStore.Video.Media.SIZE,
            MediaStore.Video.Media.ARTIST,
            MediaStore.Video.Media.DATA
        )
        val order = MediaStore.Files.FileColumns._ID + "DESC"
// 开始查询
        val cursor = contentResolver.query(tableUrl, projection, null, null, null);
        if (cursor != null) {
            // 获取id字段是第几列，该方法最好在循环之前做好
            val name = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME)
            // 获取data字段是第几列，该方法最好在循环之前做好
            val dataIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)

            while (cursor.moveToNext()) {
                val name = cursor.getString(name);
                // 获取到每张图片的绝对路径
                val path = cursor.getString(dataIndex);
                if (name.endsWith(".mp4")) {
                    Log.d(TAG, "name: $name")
                }
            }
            cursor.close();
        }
    }
}